package com.gxa.pingan.controller;

import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.pojo.PointManager;
import com.gxa.pingan.pojo.SysPoint;
import com.gxa.pingan.service.PointManagerService;
import com.gxa.pingan.service.SysPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/point")
@CrossOrigin
@Api(value = "执行积分操作的控制器",tags = "积分接口的api")
public class PointController {

    @Autowired
    private SysPointService sysPointService = null;
    /**
     * 添加系统积分
     * @Author  lirucong
     * @Date 18:58 2021/10/25
     */
    @ApiOperation("添加系统积分")
    @PostMapping("/add")
    public Map addPoint(@RequestBody SysPoint sysPoint) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        sysPointService.add(sysPoint);
        jsonResult.setCode("0");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }


    /**
     * 修改积分管理表
     * @Author  lirucong
     * @Date 18:57 2021/10/25
     */
    @ApiOperation("修改积分管理表")
    @PostMapping("/updatePG")
    public Map updatePointManager(PointManager pointManager) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    /**
     * 获取系统积分分页列表
     * @Author  lirucong
     * @Date 18:57 2021/10/25
     */
    @ApiOperation("获取系统积分分页列表")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "page", value = "页码",paramType = "Long"),
            @ApiImplicitParam(name = "id", value = "每页的记录条数",paramType = "Long")
            })
    @GetMapping("/getSysPoints")
    public Map getSysPoints(Integer page,Integer size) {
        List<SysPoint> data = sysPointService.selectByPage(page, size);
        Integer count = sysPointService.dataCount();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(data);
        jsonResult.put("count",count);
        jsonResult.setCode("0");
        jsonResult.setMsg("查询成功");
        return jsonResult.getDataMap();
    }

    /**
     * 删除系统积分
     * @Author  lirucong
     * @Date 18:59 2021/10/25
     */
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "需要删除的系统积分id",paramType = "Long")}
    )
    @ApiOperation("删除系统积分")
    @PostMapping("/delete")
    public Map delete(Long id) throws BusinessException {
        sysPointService.delete(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }

    @Autowired
    private PointManagerService pointManagerService = null;

    @PostMapping("/setPoint")
    @ApiOperation(value = "修改或添加一条用户的积分记录", notes = "修改或添加一条用户的积分记录", httpMethod = "post")
    public Map add(@RequestBody PointManager pointManager) throws BusinessException {
        pointManagerService.setPoint(pointManager);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }
    @ApiOperation(value = "获取管理用户积分的分页列表", notes = "获取管理用户积分的分页列表", httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码"),
            @ApiImplicitParam(name = "size",value = "一页的记录数"),
            @ApiImplicitParam(name = "condition",value = "查询条件")
    })
    @GetMapping("/getList")
    public Map getPointList(Integer page,Integer size,String condition) {
        List<Map<String, Object>> data = pointManagerService.getList(page, size, condition);
        Integer count = pointManagerService.getListCount(condition);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("成功");
        jsonResult.setCode("0");
        jsonResult.setData(data);
        jsonResult.put("count",count);

        return jsonResult.getDataMap();
    }
}
